//
//  songDetailsViewController.m
//  cwRadio
//
//  Created by Niels Buekers on 12/11/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "songDetailsViewController.h"
#import "cwRadioAppDelegate.h"


@implementation songDetailsViewController
@synthesize song,tabel1,imgView;

#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
    }
    return self;
}
*/


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];


	//cover
	UIImage *footerImage = [song cover];
	UIImageView *fView = [[UIImageView alloc] initWithImage:footerImage];
	[fView setFrame:CGRectMake(0.0,0.0, 128, 128)];	
	
	//buyitunes
	UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button addTarget:self action:@selector(buyItem) forControlEvents:UIControlEventTouchDown];
	UIImage *buttonImage = [UIImage imageNamed:@"itunes.jpg"];
    button.frame = CGRectMake(0,132, buttonImage.size.width, buttonImage.size.height);
	[button setImage:buttonImage forState:UIControlStateNormal];
	UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(96, 0, 320, 156)];
	[aView addSubview:button];
	[aView addSubview:fView];
	[self.tabel1 setTableFooterView:aView];
	[fView release];
	[footerImage release];
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	return 1;
}

-(IBAction) backClicked{
	[self dismissModalViewControllerAnimated:TRUE]; 
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	if(indexPath.section == 0)
			[[cell textLabel] setText:song.title];
		else if(indexPath.section == 1)
			[[cell textLabel] setText:song.artist];
	else
		[[cell textLabel] setText:song.album];
    return cell;
}

//setting the tableview with titles
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	NSString *key;
	if(section == 0)
		key = @"Title";
	else if(section == 1)
		key = @"Artist";
	else
		key = @"Album";
	return key;
}

//accessing iTunes for buying music
- (void) buyItem{
	NSString *buyString=@"itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/com.apple.jingle.search.DirectAction/search?term=";
	buyString = [buyString stringByAppendingString:song.artist];
	buyString = [buyString stringByAppendingString:@" "];
	buyString = [buyString stringByAppendingString:song.title]; 
	NSURL *url = [[NSURL alloc] initWithString:[buyString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
	[[UIApplication sharedApplication] openURL:url];
}
#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	[self buyItem];

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

